<?php

/**
 *  合同管理
 *  其他接口用。杂七杂八的

 */

namespace app\api\controller\contractmanage;

use think\Controller;
use PhpOffice\PhpWord\TemplateProcessor;
use app\api\controller\common\CommonPlus;

// use think\console\command\make\Controller;
use Phpqrcode\phpqrcode;
use think\Db;

class Commonxc extends Controller
{

    // <s></s>
    protected $admin;
    protected $model = null;
    protected $statusArr = [0=>'未处理',1=>'通过',2=>'拒绝'];
    protected $backArr = [0=>'未回传',1=>'已回传'];
    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = model('Contractmgr');
    }

    /**
     * 合同管理列表页
     * 模糊查询需后补 暂时先做普版 原先
     */
    public function contractList999()
    {
        // Vendor('phpqrcode.phpqrcode');
        // $object = new \QRcode();
        //var_dump(23);die;
        $keyword = input('keyword');
        $id_zs = input('id_zs'); // 展商id
        $id_zhui = input('id_zhui'); //展会id
        $page = input('page');
        $where = array();
        $page = max(1, intval($page));
        $pagesize = 10;

        $where['is_delete'] = 0;
        if (!empty($id_zs)) {
            $where['id_zs'] =  $id_zs;
        }
        if (!empty($id_zhui)) {
            $where['id_zhui'] =  $id_zhui;
        }


        if (!empty($keyword)) {
            $where['title|title_en'] = array('like', '%' . $keyword . '%');
        }

        $list = $this->model->listWherePage($where, 'id desc', ($page - 1) * $pagesize, $pagesize);
        $total = $this->model->countWhere($where);

        return json(['code' => 1, 'msg' => 'success', 'data' => ['list' => $list, 'total' => $total, 'countpage' => ceil($total / $pagesize)]]);
    }



    /**
     *  查询合同模版列表 供存档用
     * 主要  添加合同要生成模版。有新模版就继续手动编辑变量。然后 上传 追来源
     * com sel  一些基础传参
     */
    public function sel_htmode()
    {
        $com = new CommonPlus();
        $where = array('is_delete' => 0);
        $r = $com->sel_com_fxlist("f", $where, "contract_mould_yc");

        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功', 'data' => $r]);
    }


    /**
     *   邮箱  废弃 其他
     *http://repolist.top/index.php?s=/api/contractmanage/Commonxc/sendemail
     *

     */
    public function sendemail(){
        require_once EXTEND_PATH . "helpers/public_helperq.php";
        $emails = $this->request->param('emails');
        $emails=explode(',', $emails);
        // var_dump( $emails);die;
        $message = "你好吗测试 测试 ，<a target='_blank' href='url'>请点击 邮箱账号</a>";
        //$email="184511852@qq.com";
        // admin@plfasia.com  AAA_easy123456    stj@dicarl.com Jiayou2020
        $baseset=array('mailserver'=>'ssl://smtp.exmail.qq.com','mailport'=>'465','mailfrom'=>'tom.liu@plfasia.com',
            'mailauth_username'=>'tom.liu@plfasia.com','mailauth_password'=>'123456789aA');

        if(is_array($emails)){
            foreach ( $emails as $email ) {
                //sendmailto ( $email, $mailtitle, $mailcontent );
                sendmailto ( $email, "邮箱验证提醒", $message, '你好','',$baseset );
            }

        }else{
            sendmailto ( $emails, "邮箱验证提醒", $message, '你好','',$baseset );
        }

        //var_dump(11);die;

    }

    /**
     *  发送邮箱  在用 *
     *  tp  引入vender
     *
     *    http://repolist.top/index.php?s=/api/contractmanage/Commonxc/sendmailtpces?ids=2&emails=184511852@qq.com,737763605@qq.com,913510965@qq.com,784366227@qq.com,jiayou1632020@163.com
     *
     *
     *
     */
    public function sendmailtpces($email,$exhibition,$ex_bus_user,$flag){
        //$email邮件模板内容,$exhibition展会信息,$ex_bus_user 展商信息,$flag登录邮件1,2审核通过邮件
        $com=new commonplus();
        $emails = $this->request->param('emails');
        $emails=explode(',', $emails); // 单发

        //var_dump($emails);die;
        $time1=date('Y-m-d H:s:i');
        $success = 0;
        $error = "";

       // $ex_bus_user=array(array("toemail"=>"184511852@qq.com","toname"=>"嘉佑1845"),array("toemail"=>"913510965@qq.com","toname"=>"嘉佑916510965"));

        if(is_array($ex_bus_user)){
            foreach ( $ex_bus_user as $v ) {
                $message=$this->mailtext_ces($v,$exhibition,$flag,$email); // 格式文本内容
                $subject="2020全球自有品牌产品亚洲展&零售生鲜食材展观众预登记确认函";
               // $v['email']="184511852@qq.com";
                $data=array('user_email'=>$v['email'],'content'=>$message,'subject'=>$subject);
                $rs=$com->sendEmail([$data]);

                if ($rs) {
                    $success ++;
                }else{
                    $error .=$rs;
                }

            }


        }
        //var_dump($success);die;
        if( $success>0){
            return true;
        }else{
            return false;
        }

    }
    // 测试1200923
    public function sendmailtpces_test(){
        //$email邮件模板内容,$exhibition展会信息,$ex_bus_user 展商信息,$flag登录邮件1,2审核通过邮件
        $com=new commonplus();
        $emails = $this->request->param('emails');
        $emails=explode(',', $emails); // 单发

        //var_dump($emails);die;
        $time1=date('Y-m-d H:s:i');
        $success = 0;
        $error = "";

         $ex_bus_user=array(array("email"=>"184511852@qq.com","toname"=>"嘉佑1845"),array("email"=>"913510965@qq.com","toname"=>"嘉佑916510965"));

        if(is_array($ex_bus_user)){
            foreach ( $ex_bus_user as $v ) {

                //$message=$this->mailtext_ces($v,$exhibition,$flag,$email); // 格式文本内容

                $message=$this->mailtext_ces_01(2,$exhibition=array(),$flag=array(),$email=array()); // 格式文本内容
                //$message='你好欢迎'; // 格式文本内容
                //$data=array('user_email'=>$email,'content'=>$message); // yuanbali
                $subject="2020全球自有品牌产品亚洲展&零售生鲜食材展观众预登记确认函";
                $v['email']="184511852@qq.com";
                $data=array('user_email'=>$v['email'],'content'=>$message,'subject'=>$subject);
                $rs=$com->sendEmail([$data]);
                // var_dump($rs);die;
                if ($rs) {
                    $success ++;
                }else{
                    $error .=$rs;
                }
                // var_dump($success);die;
            }
            //   unset($v);

        }

        if( $success>0){
            //return true;
            return json(['code'=>1,'msg'=>'发送成功,共发送'.$success]);
        }else{
           // return false;
            return json(['code'=>0,'msg'=>'发送失败']);
        }

    }

    /**
     * 组合文本-测试
     *  01ad5dfcba3d3d9722f6d4aab5c56151.png
     *    http://repolist.top/public/uploads/image/
     */
    public function mailtext_ces($user_data,$exhibition,$flag,$email){
        $imgf="";
        $imgqrcode="";
        $userName=$user_data['name_cn']?$user_data['name_cn']:($user_data['name_en']?$user_data['name_en']:'预登记观众');
        $pubPath=__siteroot__.'/public/uploads/image/';//正式库和测试库切换
        $message = '<html lang="en" style="padding: 0;margin:0;">';
        $message.= '<head>';
        $message.='<meta charset="UTF-8">';
        if($flag == 1){
            $message.='<title>'.$exhibition['title'].'审核通知</title>';
        }else if($flag == 2){
            $message.='<title>'.$exhibition['title'].'登录通知</title>';
        }
        $message.='</head>';
        $message.='<body style="margin:0;padding:20px;">';
        if($flag == 1){
            $message.='<img src="'.$pubPath.$email['email_log'].'" alt="" style="display: block;width: 100%;height: auto;">';
            $message.='<div style="padding:20px;font-size:18px;font-weight: bold;text-align: center;">'.$email['email_title'].'</div>';
            $message.='<p style="margin:0;padding:10px;font-size:16px;font-weight: bold;">尊敬的'.$userName.'：</p>';
            $message.='<div style="line-height: 30px;font-size:16px;padding:10px;">感谢您登记参观<span style="font-weight: bold;">2020年第十一届全球自有品牌产品亚洲展暨零售生鲜食材展</span>。您已经成功注册，请在阅读完友情提示后打印电子确认函，请妥善保留！</div>';
            $message.='<div style="padding:30px;background: #eee;font-size:16px;font-weight: bold;text-align: center;">您的观众预登记号为：<span style="color:red;">'.$user_data['is_cart'].'</span></div>';
            $message.='<div style="padding:20px;display: flex;align-items: center;justify-content: center;">';
            //此处放二维码的文件流
            $message.='<img style="width: 220px;height: 220px" src="'.$user_data['imgebase'].'" alt="">';
            $message.='</div>';
            $message.='<p style="margin:0;padding:10px;font-size:16px;font-weight: bold;">温馨提示：</p>';
            $message.='<div style="line-height: 30px;font-size:16px;">*. 未收到胸卡的观众，也可携带打印的电子版邀请函或确认短信，在现场观众与登记专用通道，快速换取胸卡入场。访问展会官方网站 '.$email['email_ex_link'].' 了解更多展会信息。 </div>';
            $message.='<div style="line-height: 30px;font-size:16px;">*. 谢绝未成年人入场参观。</div>';
            $message.='<div style="line-height: 30px;font-size:16px;padding:10px;">期待您莅临<span style="font-weight: bold;">“2020年第十一届全球自有品牌产品亚洲展暨零售生鲜食材展”</span></div>';
            $message.='<div style="display: flex;align-items: center;justify-content: center">';
            $wx_code=json_decode($email['email_wx_code'],true);
            if(is_array($wx_code)){
                foreach ($wx_code as &$v){
                    $message.='<img style="width: 220px;height: 220px;margin-right:20px;" src="'.$pubPath.$v.'" alt="">';
                }
            }
            unset($v);
            $message.='</div>';
            $message.='<p style="margin:0;padding:10px;font-size:16px;text-align: center;">更多精彩！请关注自有品牌小程序及聚焦零售生鲜公众号</p>';
            $message.='<div style="padding:20px;font-size:18px;font-weight: bold;">展会日程</div>';
            if(is_array($exhibition['datearr'])){
                foreach ($exhibition['datearr'] as &$vv){
                    $message.='<div style="padding:10px;font-size:16px;font-weight: bold;text-align: center;">'.$vv.'</div>';
                }
            }
            unset($vv);
            $message.='<div style="padding:20px;font-size:18px;font-weight: bold;">展会地点</div>';
            $message.='<div style="padding:10px;font-size:16px;font-weight: bold;text-align: center;">'.$email['email_ex_address'].'</div>';
            $message.='<div style="padding:20px;display: flex;align-items: center;justify-content: center;">';
            $message.='<img style="width: 500px;height: 500px" src="'.$pubPath.$email['email_ex_hall'].'" alt="">';
            $message.='</div>';
        }else if($flag == 2){
            $message.='<img src="'.$pubPath.$email['email_log'].'" alt="" style="display: block;width: 100%;height: auto;">';
            $message.='<div style="padding:20px;font-size:18px;font-weight: bold;text-align: center;">观众注册验证码</div>';
            $message.='<p style="margin:0;padding:10px;font-size:16px;font-weight: bold;">尊敬的'.$userName.'：</p>';
            $message.='<div style="line-height: 30px;font-size:16px;padding:10px;">感谢您登记参观<span style="font-weight: bold;">“'.$exhibition['title'].'”</span>。您的登录码为'.$user_data['code'].'，请勿泄漏他人！</div>';
            $message.='<div style="line-height: 30px;font-size:16px;">访问展会官方网站 '.$email['email_ex_link'].' 了解更多展会信息。 </div>';
            $message.='<div style="line-height: 30px;font-size:16px;padding:10px;">期待您莅临<span style="font-weight: bold;">“2020年第十一届全球自有品牌产品亚洲展暨零售生鲜食材展”</span></div>';
        }
        $message.='</body>';
        $message.='</html>';
        return $message;
    }

    /**
     *   组合文本-测试011111
     *  01ad5dfcba3d3d9722f6d4aab5c56151.png
     *    http://repolist.top/public/uploads/image/
     */
    public function mailtext_ces_01($user_data,$exhibition,$flag,$email){
        $imgf="";
        $imgqrcode="";
        $userName=('预登记观众');
        $pubPath=__siteroot__.'/public/uploads/image/';//正式库和测试库切换
        $message = '<html lang="en" style="padding: 0;margin:0;">';
        $message.= '<head>';
        $message.='<meta charset="UTF-8">';

        $message.='</head>';
        $message.='<body style="margin:0;padding:20px;">';

            //$message.='<img src="'.$pubPath.$email['email_log'].'" alt="" style="display: block;width: 100%;height: auto;">';
            $message.='<div style="padding:20px;font-size:18px;font-weight: bold;text-align: center;">'.'</div>';
            $message.='<p style="margin:0;padding:10px;font-size:16px;font-weight: bold;">尊敬的'.'：</p>';
            $message.='<div style="line-height: 30px;font-size:16px;padding:10px;">感谢您登记参观<span style="font-weight: bold;">2020年第十一届全球自有品牌产品亚洲展暨零售生鲜食材展</span>。您已经成功注册，请在阅读完友情提示后打印电子确认函，请妥善保留！</div>';
            $message.='<div style="padding:30px;background: #eee;font-size:16px;font-weight: bold;text-align: center;">您的观众预登记号为：<span style="color:red;">'.'</span></div>';
            $message.='<div style="padding:20px;display: flex;align-items: center;justify-content: center;">';
            //此处放二维码的文件流
            $message.='<img style="width: 220px;height: 220px" src="'. '" alt="">';
            $message.='</div>';
            $message.='<p style="margin:0;padding:10px;font-size:16px;font-weight: bold;">温馨提示：</p>';
            $message.='<div style="line-height: 30px;font-size:16px;">*. 未收到胸卡的观众，也可携带打印的电子版邀请函或确认短信，在现场观众与登记专用通道，快速换取胸卡入场。访问展会官方网站 '.' 了解更多展会信息。 </div>';
            $message.='<div style="line-height: 30px;font-size:16px;">*. 谢绝未成年人入场参观。</div>';
            $message.='<div style="line-height: 30px;font-size:16px;padding:10px;">期待您莅临<span style="font-weight: bold;">“2020年第十一届全球自有品牌产品亚洲展暨零售生鲜食材展”</span></div>';
            $message.='<div style="display: flex;align-items: center;justify-content: center">';
          //  $wx_code=json_decode($email['email_wx_code'],true);

            unset($v);
            $message.='</div>';
            $message.='<p style="margin:0;padding:10px;font-size:16px;text-align: center;">更多精彩！请关注自有品牌小程序及聚焦零售生鲜公众号</p>';
            $message.='<div style="padding:20px;font-size:18px;font-weight: bold;">展会日程</div>';

            $message.='<div style="padding:20px;font-size:18px;font-weight: bold;">展会地点</div>';
            $message.='<div style="padding:10px;font-size:16px;font-weight: bold;text-align: center;">'.'</div>';
            $message.='<div style="padding:20px;display: flex;align-items: center;justify-content: center;">';

            $message.='</div>';

        $message.='</body>';
        $message.='</html>';
        return $message;
    }



    /**
     *  发送邮箱-群发
     *  tp vender
     *
     *    http://repolist.top/index.php?s=/api/contractmanage/Commonxc/sendmailtp_group?ids=2&emails=184511852@qq.com,737763605@qq.com,913510965@qq.com,784366227@qq.com,jiayou1632020@163.com
     *  [{"toemail":"184511852@qq.com","toname":"嘉佑1845"},{"toemail":"737763605@qq.com","toname":"嘉佑737"},{"toemail":"913510965@qq.com","toname":"嘉佑9135"}]
     *
     *
     */
    public function sendmailtp_group(){
        $com=new commonplus();
        $emails = $this->request->param('emails');
        // $emails=explode(',', $emails); // 单发
        $emails=json_decode($emails, TRUE);
        //var_dump($emails);die;
        $time1=date('Y-m-d H:s:i');
        $success = 0;
        $error = "";
       // $emails=array(array("toemail"=>"184511852@qq.com","toname"=>"嘉佑1845"),array("toemail"=>"913510965@qq.com","toname"=>"嘉佑916510965"));

        if(is_array($emails)){
            foreach ( $emails as $email ) {
                //$data=array('user_email'=>$email,'content'=>'邮箱信息的内容12'.$time1);
                //$para=array('toname'=>'张三');
                $para=array('toname'=>$email['toname']);
                $message=$this->mailtext('1',$para); // 格式文本内容
                $subject="标题你好999";
                $data=array('user_email'=>$email['toemail'],'content'=>$message,'subject'=>$subject);
                $rs=$com->sendEmail([$data]);
                if ($rs) {
                    $success ++;
                }else{
                    $error .=$rs;
                }
            }

        }else{
            // 不支持单发
            $para=array('toname'=>'张三');
            $message=$this->mailtext('1',$para); // 格式内容
            $data=array('user_email'=>$email,'content'=>$message);
            $rs=$com->sendEmail([$data]);
            if ($rs) {
                $success ++;
            }else{
                $error .=$rs;
            }

        }

        if( $success>0){
            return json(['code'=>1,'msg'=>'发送成功,共发送'.$success]);

        }else{
            return json(['code'=>0,'msg'=>'发送失败']);
        }

    }

    public function sendmailtp_group_test(){
        $com=new commonplus();
        $emails = $this->request->param('emails');
        // $emails=explode(',', $emails); // 单发
        $emails=json_decode($emails, TRUE);
        //var_dump($emails);die;
        $time1=date('Y-m-d H:s:i');
        $success = 0;
        $error = "";
        $emails=array(array("toemail"=>"184511852@qq.com","toname"=>"嘉佑1845"),array("toemail"=>"913510965@qq.com","toname"=>"嘉佑916510965"));

        if(is_array($emails)){
            foreach ( $emails as $email ) {
                //$data=array('user_email'=>$email,'content'=>'邮箱信息的内容12'.$time1);
                //$para=array('toname'=>'张三');
                $para=array('toname'=>$email['toname']);
                $message=$this->mailtext('1',$para); // 格式文本内容
                $subject="标题你好999";
                $data=array('user_email'=>$email['toemail'],'content'=>$message,'subject'=>$subject);
                $rs=$com->sendEmail([$data]);
                if ($rs) {
                    $success ++;
                }else{
                    $error .=$rs;
                }
            }

        }else{
            // 不支持单发
            $para=array('toname'=>'张三');
            $message=$this->mailtext('1',$para); // 格式内容
            $data=array('user_email'=>$email,'content'=>$message);
            $rs=$com->sendEmail([$data]);
            if ($rs) {
                $success ++;
            }else{
                $error .=$rs;
            }

        }

        if( $success>0){
            return json(['code'=>1,'msg'=>'发送成功,共发送'.$success]);

        }else{
            return json(['code'=>0,'msg'=>'发送失败']);
        }

    }

    /**
     * *  发送邮箱-单发
     *  tp  vender
     *
     *    http://repolist.top/index.php?s=/api/contractmanage/Commonxc/sendmailtp_one?ids=2&toemail=184511852@qq.com&toname=嘉佑

     *
     */
    public function sendmailtp_one(){
        $com=new commonplus();
        $toemail = $this->request->param('toemail');
        $toname = $this->request->param('toname');

        $time1=date('Y-m-d H:s:i');
        $success = 0;
        $error = "";
        $para=array('toname'=>$toname);
        $message=$this->mailtext('1',$para); // 格式内容
        $data=array('user_email'=>$toemail,'content'=>$message);
        $rs=$com->sendEmail([$data]);
        if ($rs) {
            $success ++;
        }else{
            $error .=$rs;
        }


        if( $success>0){
            return json(['code'=>1,'msg'=>'发送成功,共发送'.$success]);

        }else{
            return json(['code'=>0,'msg'=>'发送失败']);
        }

    }


    /**
     * 邮箱组合文本
     *  01ad5dfcba3d3d9722f6d4aab5c56151.png
     *  <img src="https://repolist.top/public/mailpic/img1.png" alt="测试bai图片" />
     *  预制图
     */
    public function mailtext($type,$item){
        $imgf="";
        $imgqrcode="";

        $message = '<html>
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>nihao</title>
		</head>
        <body>';
        $message .=' 尊敬的,'.$item['toname'].'<br>
           感谢您登记参观“2020第十一届全球自有品牌产品亚洲展暨零售生鲜食材展”。您已经成<br>
           3333<br>
        这封邮件由系统自动发送，请不要回复。
        <p align="center"> <img src='.$imgf.' alt="文件流" /></p>
        <p align="center"> <img src='.$imgqrcode.' alt="文件流" /></p>
        <p align="center"> <img src="'.__siteroot__.'/public/mailpic/img1.png" alt="文件流" /></p>

		</body>
        </html> ';
        return $message;
    }





    //  http://repolist.top/index.php?s=/api/contractmanage/Commonxc/test?id2=2&id=5&pwd=123456
    // 修改密码
    public function test(){
        $com=new commonplus();
        $id = $this->request->param('id');
        $mobile= $this->request->param('mobile');
        $kctypeid= $this->request->param('kctypeid');
        $result=array('kctypeid'=>$kctypeid,'mobile'=>$mobile);
        return json(['code'=>1,'msg'=>'成功','result'=>$result]);
        var_dump($kctypeid);die;
        $tbname="zh_20200528fmr";
        $data2=array('pwd'=>md5($pwd),'account'=>'cccccccccc');
        $where2=array('id'=>$id,'company_name'=>'test');
        $res=$com->edit_com_fxlist($data2, $where2, $tbname);
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }else{
            return json(['code'=>1,'msg'=>'成功']);
        }
    }



    // the ed

}
